<?php

class People_FamilyController extends Base_Controller_Action
{
    
    public function floorAction()
    {
        $group = new Model_Group();
        try {
            $group->read($this->_getParam('aid'));
            if (!$group->getStreet()->code) {
                throw new Base_Exception('Floor not exist');
            }
        }
        catch (Base_Exception $e) {
            return $this->_helper->ErrorPage($e->getMessage());
        }
        
        //family list
        $dbTable = new Table_Person();
        $db = $dbTable->getAdapter();
        #$whereArray['`vid` = ?'] = $group->getVillage()->vid;
        #$whereArray['`gid` = ?'] = $group->gid;
        #$entries = $dbTable->view($whereArray, array('fid ASC'));
        
        $sql = "SELECT `pid`,`fid`,`name`,`measure`,`management_type`"
             . "FROM `people`"
             . "WHERE `people`.`vid` = '".$group->getVillage()->vid."' AND `people`.`gid` = '".$group->gid."' AND `master` = '1'"
             . "GROUP BY `people`.`fid`"
             . "ORDER BY `people`.`fid` ASC";
        $result = $dbTable->getAdapter()
            ->query($sql);
        $entries = $result->fetchAll();
        
        $codes = explode(',', $group->getStreet()->code);
        $offset = 0;// - array_sum($codes);
        
        foreach ($codes as $code) {
            $offsets[] = $offset;
            $offset += $code;
        }
        
        $floorArray = array();
        foreach ($entries as $family) {
            $floorNumber = array(
                substr($family['fid'], $offsets[0], $codes[0]),
                (int) substr($family['fid'], $offsets[1], $codes[1]),
                (int) substr($family['fid'], $offsets[2], $codes[2]),
                (int) substr($family['fid'], $offsets[3], $codes[3]),
                (int) substr($family['fid'], $offsets[4], $codes[4])
            );
            if (false === array_search('0', $floorNumber)
                && $floorNumber[2]< 10 && $floorNumber[4]< 4) {
                $floorArray[] = array($floorNumber,$family);
                //$floorArray[$floorNumber[1]][$floorNumber[2]][$floorNumber[3]][$floorNumber[4]] = $family;
            }
        }
        
        $this->view->group = $group;
        $this->view->entries = $floorArray;
        #$this->_helper->layout()->setLayout('browse');
        
        //question list
        $dbTable = new Table_Floor();
        //$whereArray['`vid` = ?'] = $group->vid;
        $where = array("`people_floor`.`vid` = '".$group->vid."' AND `people_floor`.`gid` = '".$group->gid."'");
        $this->view->floors = $dbTable->view($where, 'number ASC');
    }

    public function browseAction()
    {
        $person = new Model_Person();
        try {
            $person->read($this->_getParam('pid'));
        }
        catch (Base_Exception $e) {
            return $this->_helper->ErrorPage($e->getMessage());
        }
        
        //question list
        $dbTable = new Table_Person();
        //$whereArray['`vid` = ?'] = $person->vid;
        $where = array("`people`.`vid` = '".$person->vid."' AND `people`.`gid` = '".$person->gid."' AND `people`.`fid` = '".$person->fid."'");
        //print_r($where);
        /*$sql = "SELECT *"
             . "FROM `people`"
             . "WHERE `people`.`vid` = '".$this->_getParam('vid')."' AND `people`.`gid` = '".$this->_getParam('gid')."' AND `people`.`fid_bak` = '".$this->_getParam('fid')."'";
        $result = $dbTable->getAdapter()
            ->query($sql);*/
        //$entries = $result->fetchAll();
        
        $this->view->family      = $person;
        $this->view->entries     = $dbTable->view($where, 'birth_date ASC');
        #$this->_helper->layout->setLayout('browse');
    }

    public function manageAction()
    {
        $group = new Model_Group();
        try {
            $group->read($this->_getParam('aid'));
            $village = $group->getVillage();
        }
        catch (Base_Exception $e) {
            return $this->_helper->ErrorPage(404);
        }

        //check order
        $orderBy = $this->_getParam('orderby');
        $orderFields = array('sid','created','changed');
        if (empty($orderBy) || !in_array($orderBy, $orderFields)) {
            $orderBy = 'created';
        }

        $orderWay = $this->_getParam('orderway');
        if (empty($orderWay) || !in_array($orderWay, array('ASC','DESC'))) {
            $orderWay = 'DESC';
        }

        //fetch data
        $dbTable = new Table_Person();
        Zend_Paginator::setCache(Zend_Registry::get('Zend_Cache'));
        $this->view->paginator = Zend_Paginator::factory(
                $dbTable->select()
                    ->where('`vid` = ?', $village->vid)
                    ->where('`gid` = ?', $group->gid)
                    ->where('`master` = 1')
                    ->order(array('fid ASC'))
            );

        //check page
        if (is_numeric($this->_getParam('page'))) {
            $this->view->paginator->setCurrentPageNumber($this->_getParam('page'));
        }
        
        $this->view->village = $village;
        $this->view->group   = $group;
        $this->view->request = $this->getRequest();
    }
}